package com.liao.shapping.activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.liao.shapping.MainActivity;
import com.liao.shapping.R;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView login_tv_name;
    private EditText login_et_name;
    private TextView login_tv_pwd;
    private EditText login_et_pwd;
    private CheckBox login_cb_save;
    private Button login_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        login_tv_name = (TextView) findViewById(R.id.login_tv_name);
        login_et_name = (EditText) findViewById(R.id.login_et_name);
        login_tv_pwd = (TextView) findViewById(R.id.login_tv_pwd);
        login_et_pwd = (EditText) findViewById(R.id.login_et_pwd);
        login_cb_save = (CheckBox) findViewById(R.id.login_cb_save);
        login_btn = (Button) findViewById(R.id.login_btn);

        login_btn.setOnClickListener(this);
        String user = getUser();
        if (!TextUtils.isEmpty(user)) {
            String[] split = user.split("&");
            String[] n = split[0].split("=");
            String[] p = split[1].split("=");
            login_et_name.setText(n[1]);
            login_et_pwd.setText(p[1]);
            login_cb_save.setChecked(true);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn:
                login();
                break;
        }
    }

    private void login() {
        String name = login_et_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "用户名为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String pwd = login_et_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "密码为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (login_cb_save.isChecked()) {
            saveUser(name, pwd);
        }
        startActivity(new Intent(this, MainActivity.class));
    }

    private void saveUser(String username, String password) {
        try {
            FileOutputStream fos = openFileOutput("user.txt", MODE_PRIVATE);
            String user = "账号=" + username + "&密码=" + password;
            fos.write(user.getBytes());
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getUser() {
        String user = null;
        try {
            FileInputStream fis = openFileInput("user.txt");
            byte[] bytes = new byte[1024];
            fis.read(bytes);
            user = new String(bytes);
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return user;
    }
}